package com.fyzk.activity.gl;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.view.BaseLinear;

public class CenterLeftTopView extends BaseLinear{
	
	private Button mBtn_left;
	private Button mBtn_right;

	private TextView mTxt_title;
	private LinearLayout mLi_center;

	private String centerTxtStr = "";
	private int rightVisibleInt;
	private int rightImgResurce;
	private String leftText = "";

	public CenterLeftTopView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CenterLeftTopView(Context context) {
		super(context);
	}

	@Override
	protected void init(AttributeSet attrs) {
		View.inflate(context, R.layout.bxcp_topview2, this);
		if (isInEditMode()) {
			return;
		}
		if (null != attrs) {
			TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.TopTitleView);
			centerTxtStr = type.getString(R.styleable.TopTitleView_CenterText);
			leftText = type.getString(R.styleable.TopTitleView_LeftText);
			rightVisibleInt = type.getInt(R.styleable.TopTitleView_rightVisible, 0);
			rightImgResurce = type.getResourceId(R.styleable.TopTitleView_rightImg, 0);
		}
		mLi_center = (LinearLayout) findViewById(R.id.toptitle_linear_center);
		mBtn_left = (Button) findViewById(R.id.toptitle_btn_back);
		mBtn_right = (Button) findViewById(R.id.toptitle_btn_right);
		mTxt_title = (TextView) findViewById(R.id.toptitle_text_centertitle);

		if(null!=centerTxtStr&&!centerTxtStr.equals("")){
			mTxt_title.setText(centerTxtStr);
		}
//		if(null!=leftText&&!leftText.equals("")){
//			mBtn_left.setValue(leftText);
//		}
		if(rightVisibleInt == View.INVISIBLE){
			mBtn_right.setVisibility(View.INVISIBLE);
		}else if(rightVisibleInt == View.GONE){
			mBtn_right.setVisibility(View.GONE);
		}else{
			mBtn_right.setVisibility(View.VISIBLE);
		}

		if(rightImgResurce!=0){
			mBtn_right.setBackgroundResource(rightImgResurce);
		}
		
		
		mBtn_right.setOnClickListener(this);
		mBtn_left.setOnClickListener(this);
		mLi_center.setOnClickListener(this);
	}
	
	private onTopTitleClickListener titleClick = null;

	public void setOnTopTitleClickLitener(onTopTitleClickListener titleClickListener) {
		this.titleClick = titleClickListener;
	}

	/**
	 * 设置返回键的文本显示
	 * @param text
	 */
	public void setLeftText(String text){
//		mBtn_left.setValue(text);
	}
	
	public void setRightText(String text) {
		mBtn_right.setGravity(Gravity.CENTER);
		mBtn_right.setBackgroundResource(R.drawable.login_btn_bg);
		mBtn_right.setCompoundDrawables(null, null, null, null);
		mBtn_right.setTextColor(getResources().getColor(R.color.top_bg));
		mBtn_right.setText(text);
	}

	public void setRightText(int text) {
		mBtn_right.setGravity(Gravity.CENTER);
		mBtn_right.setBackgroundResource(R.drawable.login_btn_bg);
		mBtn_right.setCompoundDrawables(null, null, null, null);
		mBtn_right.setTextColor(getResources().getColor(R.color.top_bg));
		mBtn_right.setText(text);
	}

	public void setCenterText(int id) {
		mTxt_title.setText(id);
	}

	public void setCenterText(String title) {
		mTxt_title.setText(title);
	}

	public void setRightVisible(int visible) {
		mBtn_right.setVisibility(visible);
	}

	@Override
	public void onClick(View v) {
		if (titleClick == null) {
			return;
		}
		switch (v.getId()) {
		case R.id.toptitle_linear_center:
			titleClick.onCenterClick();
			break;
		case R.id.toptitle_btn_back:
			titleClick.onLeftClick();
			break;
		case R.id.toptitle_btn_right:
			titleClick.onRightClick();
			break;
		}
	}


}
